import { IGoodsPageRow, IStoreProductRow } from "@/api/modules/pointsMall";
import ApiResult from "@/api/base/ApiResult";
import { request } from "@/utils/request";
// import { IAddProductForm } from "@/components/goodsCenter/types/addProductForm";
import { IEditProductForm } from "@/components/goodsCenter/types/editProductForm";
// import CustomerApi from "@/api/customerApi";


export function useEditProduct() {
  const formRef = ref()
  const form = reactive<IEditProductForm>({} as any)
  // let backupForm: IEditProductForm

  const getForm = async (rowData: IStoreProductRow) => {
    Object.keys(rowData).forEach((key) => {
      form[key] = rowData[key]
    })
    // form.goodsId = rowData.goodsId
    // form.id = rowData.id
    // form.name = rowData.name
    // form.thumbnailUrl = rowData.thumbnailUrl
    // form.stock = rowData.stock
    // form.beforeStock = rowData.beforeStock
    // form.totalStock = rowData.totalStock
    form.addOrDeductionStockCount = rowData.addOrDeductionStockCount
    form.addOrDeductionStockCount_fe = form.addOrDeductionStockCount ?? 0
    form.addOrDeductionStockCountSymbol_fe = '+'
    // if (!rowData.memberPriceList.lengtn) {
    //   form.memberPriceList = await queryVipMemInfo()
    // }
    // if (!rowData.memberPriceList?.length) {
    //   form['memberPriceList'] = await queryVipMemInfo()
    // }
    // debugger
    // if (!form.enableMemberPrice) {
    //   form.memberPriceList?.forEach((item:any) => {
    //     item.payPrice = rowData.payPrice
    //     item.payOfCurrencyCount = rowData.payOfCurrencyCount
    //   })
    // }
    // backupForm = JSON.parse(JSON.stringify(form))
  }

  const formatData = (data: IEditProductForm) => {
    // let dataObj:any = data
    // if (!dataObj.enableMemberPrice) {
    //   dataObj.memberPriceList?.forEach((item:any) => {
    //     item.payPrice = 0
    //     item.payOfCurrencyCount = 0
    //     item.onlyMembersCanBuy = 0
    //   })
    // }

    const _form = { ...data }
    _form.addOrDeductionStockCount = Number(form.addOrDeductionStockCountSymbol_fe + (form.addOrDeductionStockCount_fe ?? 0))

    return [{ ..._form }]
  }
  // const queryVipMemInfo = async () => {
  //   const rs = await CustomerApi.queryVipMemInfo();
  //   if (rs.code == 200) {
  //     let vipData = rs.payload.map((item: any) => {
  //       return {
  //         memberId: item.id,
  //         onlyMembersCanBuy: 0,
  //         payOfCurrencyCount: '',
  //         payPrice: '',
  //       }
  //     })
  //     return [{
  //       memberId: -1,
  //       onlyMembersCanBuy: 0,
  //       payOfCurrencyCount: '',
  //       payPrice: '',
  //     }, ...vipData]
  //   }
  // };
  const submit = (url: string, data: any, options?: any) => {
    return new Promise((resolve) => {
      formRef.value?.validate(async (valid: boolean, fields: any) => {
        if (valid) {
          const _form = formatData(data)
          const rs: ApiResult<any> = await request(url, _form)

          if (rs.code === 200) {
            ElMessage.success('提交成功')
            resolve(rs)
          }
        }
      })
    })
  }


  const isShowEditProduct = ref(false)
  const currentGoodsRowForEditProduct = ref<IGoodsPageRow>()
  const currentProductRowForEditProduct = ref<IStoreProductRow>()
  const showEditProduct = (rowData: IGoodsPageRow, productRowData: IStoreProductRow) => {

    isShowEditProduct.value = true
    currentGoodsRowForEditProduct.value = rowData
    currentProductRowForEditProduct.value = productRowData
  }

  return {
    formRef,
    form,
    getForm,
    submit,

    isShowEditProduct,
    currentGoodsRowForEditProduct,
    currentProductRowForEditProduct,
    showEditProduct
  }
}
